package itman.elm.dao.impl;

import itman.elm.dao.CartDao;
import itman.elm.po.Business;
import itman.elm.po.Cart;
import itman.elm.po.Food;
import itman.elm.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao {
    private Connection conn = null;
    private PreparedStatement pstm = null;
    private ResultSet rs = null;
    @Override
    public List<Cart> listCart(Cart cart) throws Exception {

        ArrayList<Cart> list = new ArrayList<>();

        StringBuffer sql = new StringBuffer();
        sql.append("select c.*,");
        sql.append("       f.foodId ffoodId");
        sql.append("       f.foodName ffoodName");
        sql.append("       f.foodExplain ffoodExplain");
        sql.append("       f.foodImg ffoodImp");
        sql.append("       f.foodPrice ffoodPrice");
        sql.append("       f.businessId fbusinessId");
        sql.append("       f.remarks fremarks");
        sql.append("       b.businessId bbusinessId");
        sql.append("       b.businessName bbusinessName");
        sql.append("       b.businessAddress bbusinessAddress");
        sql.append("       b.businessExplain bbusinessExplain");
        sql.append("       b.businessImp bbusinessImp");
        sql.append("       b.orderTypeId borderTypeId");
        sql.append("       b.starPrice bstarprice");
        sql.append("       b.deliveryPrice bdeliveryPrice");
        sql.append(" from (cart c left join food f on c.foodId=f.foodId)");
        sql.append("     left join business b on c.buisnessId=b.businessId)");
        sql.append(" where 1=1");

        if (cart.getBusinessId()!=null){
            sql.append(" and c.businessId="+cart.getBusinessId());
        }
        if (cart.getUserId()!=null){
            sql.append(" and c.userId='"+cart.getUserId()+"'");
        }
        try{
            conn = DBUtil.getConnection();
            pstm = conn.prepareStatement(sql.toString());

            rs = pstm.executeQuery();
            while (rs.next()){
                Cart c = new Cart();
                c.setCartId(rs.getInt("cartId"));
                c.setFoodId(rs.getInt("foodId"));
                c.setBusinessId(rs.getInt("business"));
                c.setUserId(rs.getString("userId"));
                c.setQuantity(rs.getInt("quantity"));

                Food f = new Food();
                f.setFoodId(rs.getInt("ffoodId"));
                f.setFoodName(rs.getString("ffoodName"));
                f.setFoodExplain(rs.getString("ffoodExplain"));
                f.setFoodImp(rs.getString("ffoodImp"));
                f.setFoodPrice(rs.getDouble("ffoodPrice"));
                f.setBusinessId(rs.getInt("fbusinessId"));
                f.setRemarks(rs.getString("fremarks"));
                c.setFood(f);

                Business b = new Business();
                b.setBusinessId(rs.getInt("bbusinessId"));
                b.setBusinessName(rs.getString("bbusinessName"));
                b.setBusinessAddress(rs.getString("bbusinessAddress"));
                b.setBusinessExplain(rs.getString("bbsinessExplain"));
                b.setBusinessImp(rs.getString("bbusineImp"));
                b.setOrderTypeId(rs.getInt("borderTyprId"));
                b.setStarprice(rs.getDouble("bstarprice"));
                b.setDeliveryPrice(rs.getDouble("bdeliveryPrice"));
                c.setBusiness(b);

                list.add(c);
            }
        }finally {
            DBUtil.close(rs,pstm);
        }
        return list;
    }

    @Override
    public int saveCart(Cart cart) throws Exception {
        int result = 0;
        String sql = "insert into cart values(null,?,?,?,1)";
        try {
            conn = DBUtil.getConnection();
            pstm = conn.prepareStatement(sql);
            pstm.setInt(1,cart.getCartId());
            pstm.setInt(2,cart.getBusinessId());
            pstm.setString(3, cart.getUserId());

            result = pstm.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.close(pstm);
        }
        return result;
    }

    @Override
    public int removeCart(Cart cart) throws Exception {
        int result = 0;
        StringBuffer sql = new StringBuffer("delete fome cart where 1=1");

        if(cart.getFoodId()!=null){
            sql.append(" and foodId="+cart.getFoodId());
        }
        if (cart.getBusinessId()!=null){
            sql.append(" and businessId="+cart.getBusinessId());
        }
        if (cart.getUserId()!=null) {
            sql.append(" add userId='" + cart.getUserId() + "'");
        }

            try {
                conn = DBUtil.getConnection();
                pstm = conn.prepareStatement(sql.toString());
                result = pstm.executeUpdate();
            }finally {
                DBUtil.close(pstm);
            }
        return result;
    }

    @Override
    public int updateCart(Cart cart) throws Exception {
        int result = 0;
        String sql = "update cart set quantity=? where foodId=? and businessId=? and userId=?";

    try {
        conn = DBUtil.getConnection();
        pstm = conn.prepareStatement(sql);
        pstm.setInt(1, cart.getQuantity());
        pstm.setInt(2, cart.getFoodId());
        pstm.setInt(3, cart.getBusinessId());
        pstm.setString(4, cart.getUserId());

        result = pstm.executeUpdate();
    }finally {
        DBUtil.close(pstm);
    }
    return result;
    }
}
